package com.ruoyi.web.controller.user;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.user.domain.Health;
import com.ruoyi.user.domain.Machine;
import com.ruoyi.user.domain.Sleep;
import com.ruoyi.user.domain.Step;
import com.ruoyi.user.service.IHealthService;
import com.ruoyi.user.service.IMachineService;
import com.ruoyi.user.service.ISleepService;
import com.ruoyi.user.service.IStepService;
import com.ruoyi.web.controller.user.response.HealthAnalysisResponseVo;
import com.ruoyi.web.controller.user.response.StepWebListResponseVo;

/**
 * 设备 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-12-23
 */
@Controller
@RequestMapping("/data")
public class DataCenterController extends BaseController
{
    private String prefix = "data";
    
    @Autowired
	private IHealthService healthService;
    @Autowired
	private ISleepService sleepService;
    @Autowired
	private IStepService stepService;
    @Autowired
	private IMachineService machineService;
	
	@RequiresPermissions("data:step")
	@GetMapping("/step")
	public String step()
	{
	    return prefix + "/step";
	}
	
	@GetMapping("/index")
	public String index()
	{
	    return prefix + "/index";
	}
	
	/**
	 * 步数统计
	 */
	@RequiresPermissions("data:stepList")
	@PostMapping("/stepList")
	@ResponseBody
	public TableDataInfo stepList(String date)
	{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if(date==null||"".equals(date)){
			date = sdf.format(new Date());
		}
		LinkedList<StepWebListResponseVo> listRes = new LinkedList<StepWebListResponseVo>();
		List<Step> list = stepService.selectStepWebList(date);
		for(Step step:list){
			Integer weight = 60;
			String name = "-";
			Machine param = new Machine();
			param.setCode(step.getImei());
			List<Machine> listM = machineService.selectMachineList(param);
			if(listM!=null&&listM.size()>0){
				Machine ma = listM.get(0);
				if(ma.getWeight()!=null&&!"".equals(ma.getWeight())){
					weight = Integer.valueOf(ma.getWeight());
				}
				if(ma.getNickName()!=null&&!"".equals(ma.getNickName())){
					name = ma.getNickName();
				}
			}
			StepWebListResponseVo vo = new StepWebListResponseVo();
			vo.setImei(step.getImei());
			vo.setStep(step.getPedometer());
			vo.setTotalKa(weight, step.getPedometer());
			vo.setName(name);
			listRes.add(vo);
		}
		return getDataTable(listRes);
	}
	
	@RequiresPermissions("data:heartBeat")
	@GetMapping("/heartBeat")
	public String heartBeat()
	{
	    return prefix + "/heartBeat";
	}
	
	/**
	 * 查询APP用户列表
	 */
	@RequiresPermissions("data:heartBeatList")
	@PostMapping("/heartBeatList")
	@ResponseBody
	public TableDataInfo heartBeatList(String date)
	{
		startPage();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if(date==null||"".equals(date)){
			date = sdf.format(new Date());
		}
		HealthAnalysisResponseVo vo = new HealthAnalysisResponseVo();
		vo.setName("心率偏快");
		HealthAnalysisResponseVo vo1 = new HealthAnalysisResponseVo();
		vo1.setName("心率偏慢");
		HealthAnalysisResponseVo vo2 = new HealthAnalysisResponseVo();
		vo2.setName("心率正常");
		HealthAnalysisResponseVo vo3 = new HealthAnalysisResponseVo();
		vo3.setName("心率过快");
		List<Health> list = healthService.selectHealthAnalysisList(date);
		/*心率过快> 160 >心率偏快 >110>正常>55>心率偏慢*/
		if(list!=null&&list.size()>0){
			int totalCount = list.size();
			int v = 0;//心率偏快
			int v1 = 0;//心率偏慢
			int v2 = 0;//正常
			int v3 = 0;//心率过快
			for(Health hl:list){
				if(hl.getHeartRate().intValue()>160){//心率过快
					v3++;
				}else if(hl.getHeartRate().intValue()>110){//心率偏快
					v++;
				}else if(hl.getHeartRate().intValue()>55){//正常
					v2++;
				}else{//心率偏慢
					v1++;
				}
			}
			BigDecimal tt = new BigDecimal(totalCount);
			BigDecimal b1 = new BigDecimal(100L);
	        BigDecimal t = new BigDecimal(v);
	        BigDecimal t1 = new BigDecimal(v1);
	        BigDecimal t2 = new BigDecimal(v2);
	        BigDecimal t3 = new BigDecimal(v3);
	        String p = t.divide(tt, 2, RoundingMode.HALF_UP).multiply(b1).toString()+"%";
	        String p1 = t1.divide(tt, 2, RoundingMode.HALF_UP).multiply(b1).toString()+"%";
	        String p2 = t2.divide(tt, 2, RoundingMode.HALF_UP).multiply(b1).toString()+"%";
	        String p3 = t3.divide(tt, 2, RoundingMode.HALF_UP).multiply(b1).toString()+"%";
			vo.setNum(String.valueOf(v));
			vo.setPercentage(p);
			vo1.setNum(String.valueOf(v1));
			vo1.setPercentage(p1);
			vo2.setNum(String.valueOf(v2));
			vo2.setPercentage(p2);
			vo3.setNum(String.valueOf(v3));
			vo3.setPercentage(p3);
		}else{
			vo.setNum("-");
			vo.setPercentage("-");
			vo1.setNum("-");
			vo1.setPercentage("-");
			vo2.setNum("-");
			vo2.setPercentage("-");
			vo3.setNum("-");
			vo3.setPercentage("-");
		}
		LinkedList<HealthAnalysisResponseVo> listRes = new LinkedList<HealthAnalysisResponseVo>();
		listRes.add(vo);
		listRes.add(vo1);
		listRes.add(vo2);
		listRes.add(vo3);
		return getDataTable(listRes);
	}
	
	/**
	 * 血压统计列表
	 */
	@RequiresPermissions("data:bloodList")
	@PostMapping("/bloodList")
	@ResponseBody
	public TableDataInfo bloodList(String date)
	{
		startPage();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if(date==null||"".equals(date)){
			date = sdf.format(new Date());
		}
		HealthAnalysisResponseVo vo = new HealthAnalysisResponseVo();
		vo.setName("1级高血压");
		HealthAnalysisResponseVo vo1 = new HealthAnalysisResponseVo();
		vo1.setName("2级高血压");
		HealthAnalysisResponseVo vo2 = new HealthAnalysisResponseVo();
		vo2.setName("3级高血压");
		HealthAnalysisResponseVo vo3 = new HealthAnalysisResponseVo();
		vo3.setName("正常");
		List<Health> list = healthService.selectHealthAnalysisListOne(date);
		/*1级高血压(轻度) 收缩压140~159mmHg；舒张压90~99mmHg。2级高血压(中度) 收缩压 160~179mmHg；舒张压100~109mmHg。3级高血压(重度)收缩压≥180mmHg；舒张压≥110mmHg */
		if(list!=null&&list.size()>0){
			int totalCount = list.size();
			int v = 0;//1级高血压(轻度)
			int v1 = 0;//2级高血压(中度)
			int v2 = 0;//3级高血压(重度)
			int v3 = 0;//正常
			for(Health hl:list){
				if((hl.getSdp().intValue()>=140&&hl.getSdp().intValue()<=159)&&(hl.getDbp().intValue()>=90&&hl.getDbp().intValue()<=99)){//1级高血压(轻度)
					v++;
				}else if((hl.getSdp().intValue()>=160&&hl.getSdp().intValue()<=179)&&(hl.getDbp().intValue()>=100&&hl.getDbp().intValue()<=109)){//2级高血压(中度)
					v1++;
				}else if((hl.getSdp().intValue()>=180)&&(hl.getDbp().intValue()>=110)){//3级高血压(重度)
					v2++;
				}else{//正常
					v3++;
				}
			}
			BigDecimal tt = new BigDecimal(totalCount);
			BigDecimal b1 = new BigDecimal(100L);
	        BigDecimal t = new BigDecimal(v);
	        BigDecimal t1 = new BigDecimal(v1);
	        BigDecimal t2 = new BigDecimal(v2);
	        BigDecimal t3 = new BigDecimal(v3);
	        String p = t.divide(tt, 2, RoundingMode.HALF_UP).multiply(b1).toString()+"%";
	        String p1 = t1.divide(tt, 2, RoundingMode.HALF_UP).multiply(b1).toString()+"%";
	        String p2 = t2.divide(tt, 2, RoundingMode.HALF_UP).multiply(b1).toString()+"%";
	        String p3 = t3.divide(tt, 2, RoundingMode.HALF_UP).multiply(b1).toString()+"%";
			vo.setNum(String.valueOf(v));
			vo.setPercentage(p);
			vo1.setNum(String.valueOf(v1));
			vo1.setPercentage(p1);
			vo2.setNum(String.valueOf(v2));
			vo2.setPercentage(p2);
			vo3.setNum(String.valueOf(v3));
			vo3.setPercentage(p3);
		}else{
			vo.setNum("-");
			vo.setPercentage("-");
			vo1.setNum("-");
			vo1.setPercentage("-");
			vo2.setNum("-");
			vo2.setPercentage("-");
			vo3.setNum("-");
			vo3.setPercentage("-");
		}
		LinkedList<HealthAnalysisResponseVo> listRes = new LinkedList<HealthAnalysisResponseVo>();
		listRes.add(vo);
		listRes.add(vo1);
		listRes.add(vo2);
		listRes.add(vo3);
		return getDataTable(listRes);
	}
	
	@RequiresPermissions("data:blood")
	@GetMapping("/blood")
	public String blood()
	{
	    return prefix + "/blood";
	}
	
	@RequiresPermissions("data:sleep")
	@GetMapping("/sleep")
	public String sleep()
	{
	    return prefix + "/sleep";
	}
	
	/**
	 * 血压统计列表
	 */
	@RequiresPermissions("data:sleepList")
	@PostMapping("/sleepList")
	@ResponseBody
	public TableDataInfo sleepList(String date)
	{
		startPage();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if(date==null||"".equals(date)){
			date = sdf.format(new Date());
		}
		HealthAnalysisResponseVo vo = new HealthAnalysisResponseVo();
		vo.setName("睡眠质量一般");
		HealthAnalysisResponseVo vo1 = new HealthAnalysisResponseVo();
		vo1.setName("睡眠质量差");
		HealthAnalysisResponseVo vo2 = new HealthAnalysisResponseVo();
		vo2.setName("睡眠质量良好");
		Sleep param = new Sleep();
		param.setUpdateBy(date);//SleepDate借用updateBy字段
		List<Sleep> list = sleepService.selectSleepList(param);
		/*深度睡眠-2小时以上 ->为睡眠质量好;深度睡眠-1-2小时以上 ->为睡眠质量一般;深度睡眠-1小时以上 ->为睡眠质量差*/
		if(list!=null&&list.size()>0){
			int totalCount = list.size();
			int v = 0;//睡眠质量一般
			int v1 = 0;//睡眠质量差
			int v2 = 0;//睡眠质量良好
			for(Sleep sl:list){
				if(sl.getDeepCount().intValue()>2*60){//睡眠质量一般:深度睡眠-2小时以上
					v++;
				}else if(sl.getDeepCount().intValue()>1*60){//睡眠质量差:深度睡眠-1-2小时以上
					v1++;
				}else{//睡眠质量良好:深度睡眠-1小时以上
					v2++;
				}
			}
			BigDecimal tt = new BigDecimal(totalCount);
			BigDecimal b1 = new BigDecimal(100L);
	        BigDecimal t = new BigDecimal(v);
	        BigDecimal t1 = new BigDecimal(v1);
	        BigDecimal t2 = new BigDecimal(v2);
	        String p = t.divide(tt, 2, RoundingMode.HALF_UP).multiply(b1).toString()+"%";
	        String p1 = t1.divide(tt, 2, RoundingMode.HALF_UP).multiply(b1).toString()+"%";
	        String p2 = t2.divide(tt, 2, RoundingMode.HALF_UP).multiply(b1).toString()+"%";
			vo.setNum(String.valueOf(v));
			vo.setPercentage(p);
			vo1.setNum(String.valueOf(v1));
			vo1.setPercentage(p1);
			vo2.setNum(String.valueOf(v2));
			vo2.setPercentage(p2);
		}else{
			vo.setNum("-");
			vo.setPercentage("-");
			vo1.setNum("-");
			vo1.setPercentage("-");
			vo2.setNum("-");
			vo2.setPercentage("-");
		}
		LinkedList<HealthAnalysisResponseVo> listRes = new LinkedList<HealthAnalysisResponseVo>();
		listRes.add(vo);
		listRes.add(vo1);
		listRes.add(vo2);
		return getDataTable(listRes);
	}
	
	@RequiresPermissions("data:heartFlow")
	@GetMapping("/heartFlow")
	public String heartFlow()
	{
	    return prefix + "/heartFlow";
	}
	
	@RequiresPermissions("data:position")
	@GetMapping("/position")
	public String position()
	{
	    return prefix + "/position";
	}
	
	@RequiresPermissions("data:house")
	@GetMapping("/house")
	public String house()
	{
	    return prefix + "/house";
	}
	
}
